package test;

import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

import user.User;

@SuppressWarnings("serial")
public class LogInFrame extends JFrame implements ActionListener {
	JLabel nl,pl;
	JTextField name,pass;
	JButton button1,button2;
	
	public LogInFrame() {
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setBounds(200,200,300,200);
		
		FlowLayout flow = new FlowLayout();
		JPanel main = new JPanel(flow);
		JPanel panel1=new JPanel(flow);
		JPanel panel2=new JPanel(flow);
		main.add(panel1);
		main.add(panel2);
		add(main);
		//setContentPane(panel1);
		
		nl=new JLabel("Name: ");
		panel1.add(nl);
		name = new JTextField();
		name.setSize(100,20);
		name.setPreferredSize(name.getSize());
		panel1.add(name);
		
		pl=new JLabel("Password: ");
		panel2.add(pl);
		pass = new JPasswordField();
		pass.setSize(100,20);
		pass.setPreferredSize(name.getSize());
		panel2.add(pass);
		
		button1 = new JButton("Log In");
		button2 = new JButton("Register");
		button1.addActionListener(this);
		button2.addActionListener(this);
		panel1.add(button1);
		panel2.add(button2);
		
		//name.addActionListener(this);
		//pass.addActionListener(this);
		setVisible(true);
	}
	
	@Override
	public void paint(Graphics g) {
		super.paint(g);
		
		repaint();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		@SuppressWarnings("unused")
		User user;
		String usr,pw;
		JButton src = (JButton)e.getSource();
		usr=name.getText();
		pw=pass.getText();
		//System.out.println(PWCrypt.scramble(pw));
		//System.out.println(PWCrypt.unscramble(PWCrypt.scramble(pw)));
		if(src==button1)
			user=Test.getUser(usr,pw);
		else if(src==button2)
			Test.addUser(new User(usr,pw));
	}


}
